var videoFile={};
$(function () {
    getUserList();
    $("#uploadVideo").fileinput({

        language: 'zh', //设置语言

        allowedFileExtensions: ['mp3', 'mp4'],//接收的文件后缀

        uploadAsync: true, //默认异步上传

        showUpload:false, //是否显示上传按钮

        showRemove :true, //显示移除按钮

        showPreview :true, //是否显示预览

        showCaption:false,//是否显示标题

        browseClass:"btn btn-primary", //按钮样式

        dropZoneEnabled: false,//是否显示拖拽区域

        maxFileCount:10, //表示允许同时上传的最大文件个数

        enctype:'multipart/form-data',

        validateInitialCount:true,

        previewFileIcon: "<iclass='glyphicon glyphicon-king'></i>",

        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",

    })
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#userState option:selected").text("全部");
    getUserList();
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
    var videoTitle=$("#videoTitle").val().trim();
    var state=$("#userState option:selected").val();
    if($("#userState option:selected").text()=="全部"){
        state="";
    }
	$.ajax({
		url : jypath + "/video/getYmVideoKnowledge",
		type : "get",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "state":state,
            "videoTitle":videoTitle,
		},
		jsonType : "json",
		success : function(data) {
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+l.videoId+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.videoTitle+"</td>";
                    html+="<td class='center'>"+l.strCreateDate+"</td>";
                    if(l.state==1){
                        html+="<td class='center'>有效</td>";
                    }else if(l.state==0){
                        html+="<td class='center'>无效</td>";
                    }
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
            }else{
                html+="<tr><td colspan='4' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();

		}
	})
}
function setBlogrollImageName(obj){
    videoFile = obj.files[0];
}
//新增
function addAri(){
    clearData();
    JY.Model.edit("auForm_edit","上传视频",function(){
        var that=$(this);
        if(JY.Validate.form("generateForm")){
            if(videoFile.name==undefined){
                JY.Model.info("请先上传视频!");
            }else{
                if($(".file-input").hasClass("file-input-new")){
                    JY.Model.info("请先上传视频!");
                }else{
                    $(".masks").removeClass("hide");
                    $.ajax({
                        url : jypath + "/video/insertViode",
                        type : "post",
                        data:{
                            "videoTitle":$("#generateForm input[name='videoTitle']").val().trim(),
                        },
                        success : function(data) {
                            if(data.res==1){
                                var relevanceId=data.obj;
                                var formData=new FormData();
                                formData.append("videoFile",videoFile);
                                formData.append("relevanceId",relevanceId);
                                formData.append("relevanceTpyeId",9);
                                formData.append("relevanceTpyeDetail",0);
                                $.ajax({
                                    url: jypath + "/uploadVideoAndImage",
                                    type: "post",
                                    processData: false,
                                    contentType: false,
                                    data: formData,
                                    success: function (data) {
                                        that.dialog("close");
                                        $(".masks").addClass("hide");
                                        JY.Model.info("操作成功!",function(){getUserList();});
                                    }
                                })
                            }else{
                                that.dialog("close");
                                $(".masks").addClass("hide");
                            }
                        }
                    })
                }
            }
        }
    })
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edits","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/video/updateYmVideoKnowledgeState",
                type : "post",
                data:{
                    "state":states,
                    "videoIds":chks.join()
                },
                jsonType : "json",
                success : function(data) {
                    if(data.res==1){
                        that.dialog("close");
                        JY.Model.info("操作成功!",function(){getUserList();});
                    }
                }
            })
        });
    }
}
function clearData(){
    $("#generateForm input[name='videoTitle']").val("");
    $("#uploadVideo").val("");
    $(".file-input").addClass("file-input-new");

}